package org.openotdr.server.file;

import org.openotdr.server.file.xml.ChartParser;
import org.openotdr.server.file.xml.DataParser;

//import com.google.gwt.dev.util.msg.Formatter;
import com.miyake.xml.MyXmlParser;
import com.miyake.xml.Parameter;



public class  Sr4731Converter{
	private int cols;
	private int rows;
	
	private char[] trace = null;
	private String name;
	
	public Sr4731Converter(byte[] b, String name) {
		Sr4731Parser parser = new Sr4731Parser(b);
		cols = 1;
		rows = parser.getTrace().length;
		trace = parser.getTrace();
		this.name = name;
	}
	
	public String getXML() {
		MyXmlParser parser = new MyXmlParser();
		
		String[] y = new String[rows];
		for (int i = 0; i < y.length; i++) {
			int v = (int)trace[i];
			y[i] = String.format("%.2f", (double)v / -1000.0 + 100);
		}

		parser.addY(y);
		parser.setLength(rows);
		parser.setName("OpenOtdr SR-4731 File Parser");
		parser.getParameters().add(new Parameter("Wavelength", "Wavelength", "", "1550", "nm"));
		parser.getParameters().add(new Parameter("DistanceRange", "Distance Range", "", "10", "km"));
		parser.getParameters().add(new Parameter("PulseWidth", "Pulse Width", "", "100", "ns"));

		return parser.encode();
	}
	
	public String getXML2() {
		DataParser dataParser =  new DataParser() {

			@Override
			protected
			Integer getColumnCount() {
				return cols;
			}

			@Override
			protected
			Integer getRowCount() {
				return rows;
			}

			@Override
			protected String getValue(Integer row, Integer col) {			
				int v = (int)trace[row];
				String s = String.format("%.2f", (double)v / -1000.0 + 100);
				return s;
			}

			@Override
			protected String getUnit(Integer col) {
				return "km";
			}

			@Override
			protected
			Integer getTitleRow() {
				return 0;
			}

			@Override
			protected String getName() {
				return name;
			}
			
		};
		
		dataParser.setChartParser(new ChartParser() {

			@Override
			protected
			Integer getYaxis(Integer iChart) {
				return 0;
			}

			@Override
			protected
			Integer getXaxis(Integer iChart) {
				return -1;
			}

			@Override
			protected
			Integer getChartCount() {
				return 1;
			}

			@Override
			protected
			String getName(Integer iChart) {
				return "OTDR Trace";// + iChart.toString();
			}
			
		});
		
		return dataParser.getXML();
	}
}
